#!/usr/bin/python
#
#

import sys

PYTHON_VERSION = sys.version_info


import telnetlib


class utstarcomTelnet(telnetlib.Telnet):
    def __init__(self, host=None, port=0, user=None, password=None,
                    timeout=30):
        if PYTHON_VERSION < (2, 6):
            telnetlib.Telnet.__init__(self, host, port)
        else:
            telnetlib.Telnet.__init__(self, host, port, timeout)

        self.vendor = 'utstarcom'
        self.__enable = None
        self.command = None
        if not user and not password:
            self.__user = None
            self.__password = None
        elif not user:
            self.__user = None
            self.__password = password
        elif not password:
            self.__user = user
            self.__password = None
        else:
            self.__user = user
            self.__password = password
            self.Login()

    def setUser(self, user):
        self.__user = user

    def setPassword(self, password):
        self.__password = password

    def setEnable(self, enable):
        self.__enable = enable

    def Login(self):
        if self.__user and self.__password:
            #
            self.read_until("Username:")
            self.write('%s\r\n' % self.__user)
            self.read_until("Password:")
            self.write('%s\r\n' % self.__password)
            self.read_until("> ")
            #
            self.Execute('enable')
            self.Execute('conf m')
            self.Execute('show-opt all')
        else:
            raise ValueError

    def Enable(self, enable=None):
        if enable:
            self.__enable = enable
        if self.__enable:
            self.write('enable\r\n')
            self.read_until("Password: ")
            self.write('%s\r\n' % self.__enable)
            self.read_until('# ')
        else:
            raise ValueError

    def setCommand(self, command):
        self.__command = command

    def Execute(self, command):
        self.write('%s\r\n' % command)
        return self.expect([
                # 'telnet@[a-zA-Z0-9()\-]*>[ ]*$',
                # 'telnet@[a-zA-Z0-9()\-]*#[ ]*$',
                'telnet@.*>',
                'telnet@.*#',
            ])


if __name__ == '__main__':

    import getpass
    user = raw_input('Enter user name: ')  
    password= getpass.getpass('Enter password: ')
    enable = getpass.getpass('Enter enable secret: ')

    import os

    # from datetime import date
    from datetime import datetime

    storebase = '/var/www/html/devconfs/00-GEPONs'
    n = datetime.now()
    stamp = n.strftime('%Y%m%d_%H%M%S')

    addrs = [
                # ...
            ]

    for addr in addrs:
        ct = utstarcomTelnet(addr, user=user, password=password)
        print "Connected to %s!" % addr

        # ct.Enable(enable)

        print 'Getting startup config from %s...' % addr,
        config = ct.Execute('show configuration start')[2]
        # open('config.%s' % addr, 'w').write(config)
        
        storedir = '%s/%s' % (storebase, addr)
        if not os.access(storedir, os.F_OK):
            os.mkdir(storedir)

        open('%s/%s-%s-confg.txt' % (storedir, addr, stamp), 'w').write(config)
        print ' done.'

